package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.DataDict;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.DataDictService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.service.system.UserService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/cargo/factory")
public class FactoryController extends BaseController {
    @Reference
    private FactoryService factoryService;

    @Reference
    private DataDictService dataDictService;

    /**
     * 查询厂家列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/list")
    public String factoryList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize) {
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria criteria = factoryExample.createCriteria();
        criteria.andStateEqualTo("1");
        PageInfo<Factory> pageInfo = factoryService.findByPage(factoryExample,pageNum,pageSize);
        /*List<DataDict> dataDictList = dataDictService.findAllByType("生产厂家类型");*/
        List<DataDict> dataDictList = dataDictService.findAll("生产厂家类型");
        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("dataDictList",dataDictList);
        return "cargo/factory/factory-list";
    }

    /**
     * 进入增加页面
     * @param model
     * @return
     */

    @Reference
    private UserService userService;
    @RequestMapping("/toAdd")
    public String toAdd(Model model) {
        /*List<DataDict> dataDictList = dataDictService.findAllByType("生产厂家类型");*/
        List<DataDict> dataDictList = dataDictService.findAll("生产厂家类型");
        request.setAttribute("dataDictList",dataDictList);
        return "cargo/factory/factory-add";
    }

    /**
     * 添加删除厂家
     * @param factory
     * @return
     */
    @RequestMapping("/edit")
    public String edit(Factory factory) {

        // 根据id判断是添加还是修改
        if (StringUtils.isEmpty(factory.getId())) {
            // 设置id
            factory.setId(UUID.randomUUID().toString());
            // 设置创建时间
            factory.setCreateTime(new Date());
            // 更新时间默认设置
            factory.setUpdateTime(new Date());
            // 创建人
            factory.setCreateBy(getLoginUser().getId());
            // 设置默认状态为 ：1
            factory.setState(1);
            // 添加
            factoryService.save(factory);
        } else {
            // 修改
            factoryService.update(factory);
        }
        return "redirect:/cargo/factory/list";
    }

    /**
     * 进入更新数据页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id, Model model) {
        //根据购销合同id查询
        Factory factory =factoryService.findById(id);
        //保存查询结果
        model.addAttribute("factory", factory);

        /*List<DataDict> dataDictList = dataDictService.findAllByType("生产厂家类型");*/

        List<DataDict> dataDictList = dataDictService.findAll("生产厂家类型");
        request.setAttribute("dataDictList",dataDictList);

        //跳转到页面
        return "cargo/factory/factory-update";
    }

    /**
     * 删除
     */
    @GetMapping("/delete")
    public String delete(String id) {
        Factory factory = factoryService.findById(id);
        factory.setState(0);  //  状态设置为0表示伪删除
        factoryService.update(factory);
        return "redirect:/cargo/factory/list";
    }

    /**
     * 查看： http://localhost:8080/cargo/factory/toView.do?id=f
     * 提交： http://localhost:8080/cargo/factory/submit.do?id=f
     * 取消: http://localhost:8080/cargo/factory/cancel.do?id=f
     */
    @RequestMapping("/toView")
    public String toView(String id) {
        //根厂家id查询
        Factory factory = factoryService.findById(id);
        //保存查询结果
        request.setAttribute("factory", factory);
        //跳转到页面
        return "cargo/factory/factory-view";
    }
}
